package com.xyc.petmallsystem.controller;

import com.xyc.petmallsystem.pojo.Trade;
import com.xyc.petmallsystem.repository.TradeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Joel
 * @date 2021/12/25
 */

@RestController
@RequestMapping("/trade")
public class TradeController {

    @Autowired
    TradeRepository tradeRepository;

    @RequestMapping("/find")
    public List<Trade> findAll() {
        return tradeRepository.findAll();
    }

    /*
    分页
     */
    @GetMapping("/findPage/{page}/{size}")
    public Page<Trade> findPage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        PageRequest pageable = PageRequest.of(page - 1, size);
        return tradeRepository.findAll(pageable);
    }

    /*
    查询指定id
     */
    @GetMapping("/findById/{id}")
    public Trade findById(@PathVariable("id") Integer id) {
        System.out.println("要查询的id：" + id); // 输入要查询的id
        return tradeRepository.findById(id).get();
    }

    /*
    删除
     */
    @DeleteMapping("delById/{id}")
    public void delById(@PathVariable("id") Integer id) {
        tradeRepository.deleteById(id);
    }
}
